//
// Created by francklinson on 2021/7/18 AT 0:13.
//
#include <string>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs) {
        unordered_map<string, vector<string >> um;
        for (auto &i : strs) {
            string str = i;
            sort(str.begin(), str.end()); /// 用排序确定是否同位词
            um[str].push_back(i);
        }
        vector<vector<string >> ans;
        ans.reserve(um.size());
        for (auto &x:um) {
            ans.push_back(x.second);
        }
        return ans;
    }
};

int main() {
    vector<string> strs{"eat", "tea", "tan", "ate", "nat", "bat"};
    Solution solution;
    auto ans = solution.groupAnagrams(strs);
    for (auto &r:ans) {
        for (auto &x:r) {
            cout << x;
            cout << " ";
        }
        cout << endl;
    }
    return 0;
}

